<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_User_entity{
	var $id = 0;
	var $email = "";
	var $password = "";
	var $acc_code = "";
	var $acc_type = "";
	var $bid_type = "";
	var $bid_type_code = "";
	var $first_name = "";
	var $last_name = "";
	var $job_title = "";
	var $company = "";
	var $company_url = "";
	var $company_profile = "";
	var $country = "";
	var $address_line_1 = "";
	var $address_line_2 = "";
	var $city = "";
	var $state = "";
	var $zip = "";
	var $work_phone = "";
	var $mobile = "";
	var $bank_name_acc = "";
	var $bank_acc_no = "";
	var $bank_name = "";
	var $bank_address = "";
	var $swift_code = "";
	var $status = "pending";
	var $show_type = "";
	var $country_origin = "";
	var $music_genre = "";
	var $speaker_profile = "";
	var $plays_musicals = "";
	var $activated_code = "";
	var $expired_time = 0;
	var $created_date = 0;
	var $revision = "";
	var $buyer_type = "";
	
	function __destruct() {}
	function __construct() {}
    
	function init($cmd, $input)
	{
		$ci=& get_instance();
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_ACCOUNT)->result();
		}
		else if($cmd == 'email')
		{
			$result = $ci->db->where('email',$input)->get(TBL_ACCOUNT)->result();
		}
		else if($cmd == 'acc_code')
		{
			$result = $ci->db->where('acc_code',$input)->get(TBL_ACCOUNT)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->email = $result[0]->email;
			$this->password = $result[0]->password;
			$this->acc_code = $result[0]->acc_code;
			$this->acc_type = $result[0]->acc_type;
			$this->bid_type = $result[0]->bid_type;
			$this->bid_type_code = $result[0]->bid_type_code;
			$this->first_name = $result[0]->first_name;
			$this->last_name = $result[0]->last_name;
			$this->job_title = $result[0]->job_title;
			$this->company = $result[0]->company;
			$this->company_url = $result[0]->company_url;
			$this->company_profile = $result[0]->company_profile;
			$this->country = $result[0]->country;
			$this->address_line_1 = $result[0]->address_line_1;
			$this->address_line_2 = $result[0]->address_line_2;
			$this->city = $result[0]->city;
			$this->state = $result[0]->state;
			$this->zip = $result[0]->zip;
			$this->work_phone = $result[0]->work_phone;
			$this->mobile = $result[0]->mobile;
			$this->bank_name_acc = $result[0]->bank_name_acc;
			$this->bank_acc_no = $result[0]->bank_acc_no;
			$this->bank_name = $result[0]->bank_name;
			$this->bank_address = $result[0]->bank_address;
			$this->swift_code = $result[0]->swift_code;
			$this->status = $result[0]->status;
			$this->show_type = $result[0]->show_type;
			$this->country_origin = $result[0]->country_origin;
			$this->music_genre = $result[0]->music_genre;
			$this->speaker_profile = $result[0]->speaker_profile;
			$this->plays_musicals = $result[0]->plays_musicals;
			$this->activated_code = $result[0]->activated_code;
			$this->expired_time = $result[0]->expired_time;
			$this->created_date = $result[0]->created_date;
			$this->revision = $result[0]->revision;
			$this->buyer_type = $result[0]->buyer_type;
			
			// Create user folder if not exists
			$directory = "./uploads/default/users/".$result[0]->id ;
			if (!file_exists ( $directory )) {
				mkdir($directory);
				chmod( $directory, 0777 ) ;
				file_put_contents($directory."/index.php","");
				file_put_contents($directory."/index.html","");
			}
			return $this;
		}
		return false;
	}
	
	function setEmail($input){ $this->email=$input; }
	function setPassword($input){ $this->password=$input; }
	function setAccCode($input){ $this->acc_code=$input; }
	function setAccType($input){ $this->acc_type=$input; }
	function setBidType($input){ $this->bid_type=$input; }
	function setBidTypeCode($input){ $this->bid_type_code = $input; }
	function setFirstName($input){ $this->first_name=$input; }
	function setLastName($input){ $this->last_name=$input; }
	function setJobTitle($input){ $this->job_title=$input; }
	function setCompany($input){ $this->company=$input; }
	function setCompanyUrl($input){ $this->company_url=$input; }
	function setCompanyProfile($input){ $this->company_profile=$input;}
	function setCountry($input){ $this->country=$input; }
	function setAddressLine1($input){ $this->address_line_1=$input; }
	function setAddressLine2($input){ $this->address_line_2=$input; }
	function setCity($input){ $this->city = $input; }
	function setState($input){ $this->state=$input; }
	function setZip($input){ $this->zip=$input; }
	function setWorkPhone($input) { $this->work_phone=$input; }
	function setMobile($input){ $this->mobile=$input; }
	function setBankNameAcc($input){ $this->bank_name_acc=$input; }
	function setBankAccNo($input){ $this->bank_acc_no=$input; }
	function setBankName($input){ $this->bank_name=$input; }
	function setBankAddress($input) { $this->bank_address=$input; }
	function setSwiftCode($input) { $this->swift_code=$input; }
	function setStatus($input) { $this->status=$input; }
	function setShowType($input) { $this->show_type=$input; }
	function setCountryOrigin($input) {$this->country_origin = $input; }
	function setMusicGenre($input) { $this->music_genre=$input; }
	function setSpeakerProfile($input) { $this->speaker_profile = $input; }
	function setPlaysMusicals($input) { $this->plays_musicals = $input; }
	function setActivatedCode($input) { $this->activated_code=$input; }
	function setExpiredTime($input) { $this->expired_time=$input; }
	function setCreatedDate($input) { $this->created_date= $input;}
	function setRevision($input){ $this->revision = $input; }
	function setBuyerType($input){ $this->buyer_type = $input; }
	
	function getId(){ return $this->id; }
	function getEmail(){ return $this->email; }
	function getPasswd(){ return $this->password; }
	function getAccCode() { return $this->acc_code;}
	function getAccType() { return $this->acc_type;}
	function getBidType(){ return $this->bid_type;}
	function getBidTypeCode(){ return $this->bid_type_code;}
	function getFirstName(){ return $this->first_name; }
	function getLastName(){ return $this->last_name; }
	function getJobTitle(){ return $this->job_title; }
	function getCompany(){ return $this->company; }
	function getCompanyUrl(){ return $this->company_url; }
	function getCompanyProfile(){ return $this->company_profile;}
	function getCountry(){ return $this->country; }	
	function getAddressLine1(){ return $this->address_line_1; }
	function getAddressLine2(){ return $this->address_line_2; }
	function getCity(){ return $this->city; }
	function getState(){ return $this->state; }
	function getZip(){ return $this->zip; }
	function getWorkPhone(){ return $this->work_phone; }
	function getMobile(){ return $this->mobile; }
	function getBankNameAcc(){ return $this->bank_name_acc; }
	function getBankAccNo(){ return $this->bank_acc_no; }
	function getBankName(){ return $this->bank_name; }
	function getBankAddress() { return $this->bank_address; }
	function getSwiftCode() { return $this->swift_code; }
	function getStatus() { return $this->status; }
	function getShowType() { return $this->show_type; }
	function getCountryOrigin() {return $this->country_origin; }
	function getMusicGenre() { return $this->music_genre; }
	function getSpeakerProfile(){ return $this->speaker_profile; }
	function getPlaysMusicals() { return $this->plays_musicals; } 
	function getActivatedCode() { return $this->activated_code; }
	function getExpiredTime() { return $this->expired_time; }
	function getCreatedDate(){ return $this->created_date; }
	function getRevision(){ return $this->revision; }
	function getBuyerType(){ return $this->buyer_type; }
	
	function getFolderFileStorage(){ 
		return "./uploads/default/users/".$this->id."/"; 
	}
	
	function whoAmI(){
		if($this->getAccType() == 'buyer'){
			return 'buyer';
		}else{
			return 'seller';
		}
	}
		
	function checkPassword($string)
	{
		if($string == $this->password)
		{
			return true;
		}
		return false;
	}
	
	function update()
	{
		$ci=& get_instance();
		$setPassword = "";
		if($this->password != "")
		{
			$ci->db->set('password',$this->password);
		}
		$ci->db->set('email', $this->email);
		$ci->db->set('acc_code', $this->acc_code);
		$ci->db->set('acc_type', $this->acc_type);
		$ci->db->set('bid_type', $this->bid_type);
		$ci->db->set('bid_type_code', $this->bid_type_code);
		$ci->db->set('first_name', $this->first_name);
		$ci->db->set('last_name', $this->last_name);
		$ci->db->set('job_title', $this->job_title);
		$ci->db->set('company', $this->company);
		$ci->db->set('company_url', $this->company_url);
		$ci->db->set('company_profile', $this->company_profile);
		$ci->db->set('country', $this->country);
		$ci->db->set('address_line_1', $this->address_line_1);
		$ci->db->set('address_line_2', $this->address_line_2);
		$ci->db->set('city', $this->city);
		$ci->db->set('state', $this->state);
		$ci->db->set('zip', $this->zip);
		$ci->db->set('work_phone', $this->work_phone);
		$ci->db->set('mobile', $this->mobile);
		$ci->db->set('bank_name_acc', $this->bank_name_acc);
		$ci->db->set('bank_acc_no', $this->bank_acc_no);
		$ci->db->set('bank_name', $this->bank_name);
		$ci->db->set('bank_address', $this->bank_address);
		$ci->db->set('swift_code', $this->swift_code);
		$ci->db->set('status', $this->status);
		$ci->db->set('show_type', $this->show_type);
		$ci->db->set('country_origin', $this->country_origin);
		$ci->db->set('music_genre', $this->music_genre);
		$ci->db->set('speaker_profile', $this->speaker_profile);
		$ci->db->set('plays_musicals', $this->plays_musicals);
		$ci->db->set('activated_code', $this->activated_code);
		$ci->db->set('expired_time', $this->expired_time);
		$ci->db->set('created_date', $this->created_date);
		$ci->db->set('revision', $this->revision );
		$ci->db->set('buyer_type', $this->buyer_type );
		
		$ci->db->where('id', $this->id)->update(TBL_ACCOUNT);
	}
	
	function createNewUserEntity($email){
		$ci=& get_instance();
		
		$ci->db->set('email',$email);
		$ci->db->insert(TBL_ACCOUNT);
		
		return $ci->db->insert_id();
	}
	
	function buildNewRevision(){
		$old_revision = $this->revision;
		
		$revison_array = array();
		if(!empty($old_revision)){
			$tmp_array = unserialize($old_revision);
			$tmp_array = array_reverse($tmp_array);
			$i= 0;
			foreach($tmp_array as $array){
				if($i>3){break;}
				$i++;
				$revison_array[] = $array;
			}
			$revison_array = array_reverse($revison_array);
		}
	
		$new_revision = array(
			 'id' => $this->id,
			 'email' => $this->email,
			 'password' => $this->password,
			 'acc_code' => $this->acc_code,
			 'acc_type' => $this->acc_type,
			 'bid_type' => $this->bid_type,
			 'bid_type_code' => $this->bid_type_code,
			 'first_name' => $this->first_name,
			 'last_name' => $this->last_name,
			 'job_title' => $this->job_title,
			 'company' => $this->company,
			 'company_url' => $this->company_url,
			 'company_profile' => $this->company_profile,			 
			 'country' => $this->country,
			 'address_line_1' => $this->address_line_1,
			 'address_line_2' => $this->address_line_2, 
			 'city' => $this->city,
			 'state' => $this->state,
			 'zip' => $this->zip,
			 'work_phone' => $this->work_phone,
			 'mobile' => $this->mobile,
			 'bank_name_acc' => $this->bank_name_acc,
			 'bank_acc_no' => $this->bank_acc_no,
			 'bank_name' => $this->bank_name,
			 'bank_address' => $this->bank_address,
			 'swift_code' => $this->swift_code,
			 'status' => $this->status,
			 'show_type' => $this->show_type,
			 'country_origin' => $this->country_origin,
			 'music_genre' => $this->music_genre,
			 'speaker_profile' => $this->speaker_profile,
			 'plays_musicals' => $this->plays_musicals,
			 'activated_code' => $this->activated_code,
			 'expired_time' => $this->expired_time,
			 'created_date' => $this->created_date,
			 'buyer_type'	=> $this->buyer_type
		);
		
		$revison_array[] = $new_revision;
		return serialize( $revison_array );
	}
}
